{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "8d7703dc-4560-4c42-b641-c95dc1958032",
      "metadata": {
        "id": "8d7703dc-4560-4c42-b641-c95dc1958032"
      },
      "source": [
        "# Quantum Software Development Journey:\n",
        "# From Theory to Application with Classiq"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "d1af5c13-129a-4e5d-8f40-ad400d35b764",
      "metadata": {
        "id": "d1af5c13-129a-4e5d-8f40-ad400d35b764"
      },
      "source": [
        "## Final Assignment - Overview\n",
        "\n",
        "In this homework assignment, you have the opportunity to implement something meaningful, be creative, and practice your new skills and knowledge! You can apply your knowledge from the course and contribute to the Classiq community. Details are provided below.\n",
        "\n",
        "**Successful contributions will have the opportunity to earn SWAG credits and potentially more advanced certificates.**\n",
        "\n",
        "## The Project\n",
        "\n",
        "### Goal\n",
        "The main goal is to be creative, collaborate with each other and with the community, and to apply your knowledge to bridge the gap between theory and application! Focus on the last two weeks of the course, using this assignment to solidify and expand your knowledge and skills.\n",
        "\n",
        "### Optional Projects\n",
        "You may choose a project from the following list, or come up with your own idea. Just please make sure to consult with us before you start!\n",
        "\n",
        "- **Domain of Expertise**:\n",
        "  - Use your background in neural networks, chemistry, biology, or any other field to implement something new. We are here to support you!\n",
        "  - Example: An advanced application of VQE for a more complex molecule.\n",
        "  - Implementation of different QNN or QAE architectures.\n",
        "  - Combine your domain of expertise with the power of Classiq!\n",
        "\n",
        "- **QNN for XOR Problem**:\n",
        "  - Classiq has an available dataset for training PQC to imitate the XOR gate, similar to how we trained a U-gate to act as a NOT gate. Design a QNN to solve the XOR problem. Read more on the dataset [here](https://docs.classiq.io/latest/reference-manual/built-in-algorithms/qml/qnn/datasets/#datasetxor).\n",
        "\n",
        "- **QNN as VQE**:\n",
        "  - Like VQE, QNNs with well-suited loss functions and data can be used to find minima of a system. Can you solve one of the problems from week 3 (in class or HW3) by implementing it with QNN?\n",
        "  - Review the materials from weeks 3 and 4 and tackle this problem!\n",
        "  - If needed, you may create synthetic data.\n",
        "  - Do you have another example to show how QNN can generalize VQE? Show us!\n",
        "\n",
        "- **Noise Reduction Using Quantum Auto-Encoders**:\n",
        "  - Quantum Auto-Encoders can be used to reduce noise!\n",
        "  - Create code that generates a quantum state, adds random noise to it, and tries to reconstruct it using a Quantum Auto-Encoder.\n",
        "  - If needed, you may create synthetic data.\n",
        "\n",
        "- **Contribute to Quantum Algorithm Zoo**:\n",
        "  - Implement one of the algorithms in the [Quantum Algorithm Zoo](https://quantumalgorithmzoo.org/) that has not been implemented yet using Classiq.\n",
        "  - By doing so, you will have the opportunity to contribute to one of the main resources on quantum algorithms! (your Implementation will be linked to their website!)\n",
        "\n",
        "- **New Algorithm Implementation**:\n",
        "  - Choose a research paper (you may consult us) and try to implement it using Classiq's SDK.\n",
        "\n",
        "### Note\n",
        "\n",
        "- For those who choose a more extensive project, you will have a discussion with me or another Classiq member to fine-tune your project's purpose and set a deadline. This collaborative approach ensures your project aligns with course objectives and maximizes your learning experience.\n",
        "- **You are allowed to work in teams of up to 3 members!**\n",
        "\n",
        "### Deadline & Submission\n",
        "\n",
        "- **Important Dates**:\n",
        "  - **Assignment Release:** 29.5.2024\n",
        "  - **Submission Deadline:** 10.6.2024 (7 A.M GMT+3)\n",
        "- Consult with us before submitting your project, and we will direct you to the right place in the [Classiq Library](https://github.com/Classiq/classiq-library).\n",
        "- You might get an extension to the deadline based on your specific project and progress.\n",
        "\n",
        "## Conclusion\n",
        "\n",
        "Choose the project that best aligns with your interests and career goals. This project provides a valuable opportunity to deepen your understanding of QML and quantum computing in general, while contributing to the Classiq community.\n",
        "\n",
        "If you have any questions or need further clarification, feel free to reach out.\n",
        "\n",
        "**Happy coding!**\n"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# VQE for a more Complex Molecule:\n",
        "### Hamiltonian of the Water (H₂O) Molecule"
      ],
      "metadata": {
        "id": "HojnZ_UpJrDs"
      },
      "id": "HojnZ_UpJrDs"
    },
    {
      "cell_type": "markdown",
      "source": [
        "\\begin{align*}\n",
        "\\hat{H} =& - 73.13873 (I\\otimes I\\otimes I\\otimes I\\otimes I\\otimes I\\otimes I\\otimes I) + 0.22757 (Z\\otimes I\\otimes I\\otimes I\\otimes I\\otimes I\\otimes I\\otimes I)\\\\\n",
        "&- 0.04375 (Y\\otimes Z\\otimes Z\\otimes Z\\otimes Y\\otimes I\\otimes I\\otimes I) - 0.04375 (X\\otimes Z\\otimes Z\\otimes Z\\otimes X\\otimes I\\otimes I\\otimes I)\\\\\n",
        "&+ 0.17420 (I\\otimes I\\otimes Z\\otimes I\\otimes I\\otimes I\\otimes I\\otimes I) + 0.16757 (Z\\otimes I\\otimes Z\\otimes I\\otimes I\\otimes I\\otimes I\\otimes I)\\\\\n",
        "&- 0.15961 (I\\otimes I\\otimes I\\otimes I\\otimes Z\\otimes I\\otimes I\\otimes I) + 0.11952 (Z\\otimes I\\otimes I\\otimes I\\otimes Z\\otimes I\\otimes I\\otimes I)\\\\\n",
        "&- 0.18067 (I\\otimes I\\otimes I\\otimes I\\otimes I\\otimes I\\otimes Z\\otimes I) + 0.13402 (Z\\otimes I\\otimes I\\otimes I\\otimes I\\otimes I\\otimes Z\\otimes I)\\\\\n",
        "&+ 0.22757 (I\\otimes Z\\otimes I\\otimes I\\otimes I\\otimes I\\otimes I\\otimes I) + 0.19393 (Z\\otimes Z\\otimes I\\otimes I\\otimes I\\otimes I\\otimes I\\otimes I)\\\\\n",
        "&- 0.03079 (Y\\otimes I\\otimes Z\\otimes Z\\otimes Y\\otimes I\\otimes I\\otimes I) - 0.03079 (X\\otimes I\\otimes Z\\otimes Z\\otimes X\\otimes I\\otimes I\\otimes I)\\\\\n",
        "&+ 0.01387 (Y\\otimes X\\otimes X\\otimes Y\\otimes I\\otimes I\\otimes I\\otimes I) - 0.01387 (Y\\otimes Y\\otimes X\\otimes X\\otimes I\\otimes I\\otimes I\\otimes I)\\\\\n",
        "&- 0.01387 (X\\otimes X\\otimes Y\\otimes Y\\otimes I\\otimes I\\otimes I\\otimes I) + 0.01387 (X\\otimes Y\\otimes Y\\otimes X\\otimes I\\otimes I\\otimes I\\otimes I)\\\\\n",
        "&- 0.04375 (I\\otimes Y\\otimes Z\\otimes Z\\otimes Z\\otimes Y\\otimes I\\otimes I) - 0.03079 (Z\\otimes Y\\otimes Z\\otimes Z\\otimes Z\\otimes Y\\otimes I\\otimes I)\\\\\n",
        "&- 0.04375 (I\\otimes X\\otimes Z\\otimes Z\\otimes Z\\otimes X\\otimes I\\otimes I) - 0.03079 (Z\\otimes X\\otimes Z\\otimes Z\\otimes Z\\otimes X\\otimes I\\otimes I)\\\\\n",
        "&+ 0.01783 (Y\\otimes X\\otimes I\\otimes I\\otimes X\\otimes Y\\otimes I\\otimes I) - 0.01783 (Y\\otimes Y\\otimes I\\otimes I\\otimes X\\otimes X\\otimes I\\otimes I)\\\\\n",
        "&- 0.01783 (X\\otimes X\\otimes I\\otimes I\\otimes Y\\otimes Y\\otimes I\\otimes I) + 0.01783 (X\\otimes Y\\otimes I\\otimes I\\otimes Y\\otimes X\\otimes I\\otimes I)\\\\\n",
        "&+ 0.01737 (Y\\otimes X\\otimes I\\otimes I\\otimes I\\otimes I\\otimes X\\otimes Y) - 0.01737 (Y\\otimes Y\\otimes I\\otimes I\\otimes I\\otimes I\\otimes X\\otimes X)\\\\\n",
        "&- 0.01737 (X\\otimes X\\otimes I\\otimes I\\otimes I\\otimes I\\otimes Y\\otimes Y) + 0.01737 (X\\otimes Y\\otimes I\\otimes I\\otimes I\\otimes I\\otimes Y\\otimes X)\\\\\n",
        "&- 0.03010 (Y\\otimes Z\\otimes I\\otimes Z\\otimes Y\\otimes I\\otimes I\\otimes I) - 0.03010 (X\\otimes Z\\otimes I\\otimes Z\\otimes X\\otimes I\\otimes I\\otimes I)\\\\\n",
        "&+ 0.17420 (I\\otimes I\\otimes I\\otimes Z\\otimes I\\otimes I\\otimes I\\otimes I) + 0.18144 (Z\\otimes I\\otimes I\\otimes Z\\otimes I\\otimes I\\otimes I\\otimes I)\\\\\n",
        "&- 0.02981 (Y\\otimes Z\\otimes Z\\otimes I\\otimes Y\\otimes I\\otimes I\\otimes I) - 0.02981 (X\\otimes Z\\otimes Z\\otimes I\\otimes X\\otimes I\\otimes I\\otimes I)\\\\\n",
        "&- 0.00029 (Y\\otimes Z\\otimes Y\\otimes Y\\otimes Z\\otimes Y\\otimes I\\otimes I) - 0.00029 (Y\\otimes Z\\otimes Y\\otimes X\\otimes Z\\otimes X\\otimes I\\otimes I)\\\\\n",
        "&- 0.00029 (X\\otimes Z\\otimes X\\otimes Y\\otimes Z\\otimes Y\\otimes I\\otimes I) - 0.00029 (X\\otimes Z\\otimes X\\otimes X\\otimes Z\\otimes X\\otimes I\\otimes I)\\\\\n",
        "&- 0.02510 (Y\\otimes Z\\otimes Z\\otimes Z\\otimes Y\\otimes I\\otimes Z\\otimes I) - 0.02510 (X\\otimes Z\\otimes Z\\otimes Z\\otimes X\\otimes I\\otimes Z\\otimes I)\\\\\n",
        "&- 0.15961 (I\\otimes I\\otimes I\\otimes I\\otimes I\\otimes Z\\otimes I\\otimes I) + 0.13735 (Z\\otimes I\\otimes I\\otimes I\\otimes I\\otimes Z\\otimes I\\otimes I)\\\\\n",
        "&- 0.01131 (Y\\otimes Z\\otimes Z\\otimes Z\\otimes Y\\otimes Z\\otimes I\\otimes I) - 0.01131 (X\\otimes Z\\otimes Z\\otimes Z\\otimes X\\otimes Z\\otimes I\\otimes I)\\\\\n",
        "&- 0.01456 (Y\\otimes Z\\otimes Z\\otimes Z\\otimes Z\\otimes X\\otimes X\\otimes Y) + 0.01456 (Y\\otimes Z\\otimes Z\\otimes Z\\otimes Z\\otimes Y\\otimes X\\otimes X)\\\\\n",
        "&+ 0.01456 (X\\otimes Z\\otimes Z\\otimes Z\\otimes Z\\otimes X\\otimes Y\\otimes Y) - 0.01456 (X\\otimes Z\\otimes Z\\otimes Z\\otimes Z\\otimes Y\\otimes Y\\otimes X)\\\\\n",
        "&- 0.18067 (I\\otimes I\\otimes I\\otimes I\\otimes I\\otimes I\\otimes I\\otimes Z) + 0.15138 (Z\\otimes I\\otimes I\\otimes I\\otimes I\\otimes I\\otimes I\\otimes Z)\\\\\n",
        "&- 0.01054 (Y\\otimes Z\\otimes Z\\otimes Z\\otimes Y\\otimes I\\otimes I\\otimes Z) - 0.01054 (X\\otimes Z\\otimes Z\\otimes Z\\otimes X\\otimes I\\otimes I\\otimes Z)\\\\\n",
        "&+ 0.16757 (I\\otimes Z\\otimes I\\otimes Z\\otimes I\\otimes I\\otimes I\\otimes I) + 0.11952 (I\\otimes Z\\otimes I\\otimes I\\otimes I\\otimes Z\\otimes I\\otimes I)\\\\\n",
        "&+ 0.13402 (I\\otimes Z\\otimes I\\otimes I\\otimes I\\otimes I\\otimes I\\otimes Z) + 0.18144 (I\\otimes Z\\otimes Z\\otimes I\\otimes I\\otimes I\\otimes I\\otimes I)\\\\\n",
        "&- 0.02981 (I\\otimes Y\\otimes I\\otimes Z\\otimes Z\\otimes Y\\otimes I\\otimes I) - 0.02981 (I\\otimes X\\otimes I\\otimes Z\\otimes Z\\otimes X\\otimes I\\otimes I)\\\\\n",
        "&- 0.00029 (I\\otimes Y\\otimes X\\otimes X\\otimes Y\\otimes I\\otimes I\\otimes I) + 0.00029 (I\\otimes Y\\otimes Y\\otimes X\\otimes X\\otimes I\\otimes I\\otimes I)\\\\\n",
        "&+ 0.00029 (I\\otimes X\\otimes X\\otimes Y\\otimes Y\\otimes I\\otimes I\\otimes I) - 0.00029 (I\\otimes X\\otimes Y\\otimes Y\\otimes X\\otimes I\\otimes I\\otimes I)\\\\\n",
        "&- 0.03010 (I\\otimes Y\\otimes Z\\otimes I\\otimes Z\\otimes Y\\otimes I\\otimes I) - 0.03010 (I\\otimes X\\otimes Z\\otimes I\\otimes Z\\otimes X\\otimes I\\otimes I)\\\\\n",
        "&+ 0.13735 (I\\otimes Z\\otimes I\\otimes I\\otimes Z\\otimes I\\otimes I\\otimes I) - 0.01131 (I\\otimes Y\\otimes Z\\otimes Z\\otimes I\\otimes Y\\otimes I\\otimes I)\\\\\n",
        "&- 0.01131 (I\\otimes X\\otimes Z\\otimes Z\\otimes I\\otimes X\\otimes I\\otimes I) + 0.01456 (I\\otimes Y\\otimes Z\\otimes Z\\otimes X\\otimes I\\otimes X\\otimes Y)\\\\\n",
        "&+ 0.01456 (I\\otimes Y\\otimes Z\\otimes Z\\otimes Y\\otimes I\\otimes Y\\otimes Y) + 0.01456 (I\\otimes X\\otimes Z\\otimes Z\\otimes X\\otimes I\\otimes X\\otimes X)\\\\\n",
        "&+ 0.01456 (I\\otimes X\\otimes Z\\otimes Z\\otimes Y\\otimes I\\otimes Y\\otimes X) - 0.02510 (I\\otimes Y\\otimes Z\\otimes Z\\otimes Z\\otimes Y\\otimes I\\otimes Z)\\\\\n",
        "&- 0.02510 (I\\otimes X\\otimes Z\\otimes Z\\otimes Z\\otimes X\\otimes I\\otimes Z) + 0.15138 (I\\otimes Z\\otimes I\\otimes I\\otimes I\\otimes I\\otimes Z\\otimes I)\\\\\n",
        "&- 0.01054 (I\\otimes Y\\otimes Z\\otimes Z\\otimes Z\\otimes Y\\otimes Z\\otimes I) - 0.01054 (I\\otimes X\\otimes Z\\otimes Z\\otimes Z\\otimes X\\otimes Z\\otimes I)\\\\\n",
        "&+ 0.13767 (I\\otimes I\\otimes Z\\otimes I\\otimes Z\\otimes I\\otimes I\\otimes I) + 0.14973 (I\\otimes I\\otimes Z\\otimes I\\otimes I\\otimes I\\otimes Z\\otimes I)\\\\\n",
        "&+ 0.22004 (I\\otimes I\\otimes Z\\otimes Z\\otimes I\\otimes I\\otimes I\\otimes I) + 0.00956 (I\\otimes I\\otimes Y\\otimes X\\otimes X\\otimes Y\\otimes I\\otimes I)\\\\\n",
        "&- 0.00956 (I\\otimes I\\otimes Y\\otimes Y\\otimes X\\otimes X\\otimes I\\otimes I) - 0.00956 (I\\otimes I\\otimes X\\otimes X\\otimes Y\\otimes Y\\otimes I\\otimes I)\\\\\n",
        "&+ 0.00956 (I\\otimes I\\otimes X\\otimes Y\\otimes Y\\otimes X\\otimes I\\otimes I) + 0.00609 (I\\otimes I\\otimes Y\\otimes X\\otimes I\\otimes I\\otimes X\\otimes Y)\\\\\n",
        "&- 0.00609 (I\\otimes I\\otimes Y\\otimes Y\\otimes I\\otimes I\\otimes X\\otimes X) - 0.00609 (I\\otimes I\\otimes X\\otimes X\\otimes I\\otimes I\\otimes Y\\otimes Y)\\\\\n",
        "&+ 0.00609 (I\\otimes I\\otimes X\\otimes Y\\otimes I\\otimes I\\otimes Y\\otimes X) + 0.14723 (I\\otimes I\\otimes Z\\otimes I\\otimes I\\otimes Z\\otimes I\\otimes I)\\\\\n",
        "&+ 0.15582 (I\\otimes I\\otimes Z\\otimes I\\otimes I\\otimes I\\otimes I\\otimes Z) + 0.13767 (I\\otimes I\\otimes I\\otimes Z\\otimes I\\otimes Z\\otimes I\\otimes I)\\\\\n",
        "&+ 0.14973 (I\\otimes I\\otimes I\\otimes Z\\otimes I\\otimes I\\otimes I\\otimes Z) + 0.14723 (I\\otimes I\\otimes I\\otimes Z\\otimes Z\\otimes I\\otimes I\\otimes I)\\\\\n",
        "&+ 0.15582 (I\\otimes I\\otimes I\\otimes Z\\otimes I\\otimes I\\otimes Z\\otimes I) + 0.11270 (I\\otimes I\\otimes I\\otimes I\\otimes Z\\otimes I\\otimes Z\\otimes I)\\\\\n",
        "&+ 0.14926 (I\\otimes I\\otimes I\\otimes I\\otimes Z\\otimes Z\\otimes I\\otimes I) + 0.02869 (I\\otimes I\\otimes I\\otimes I\\otimes Y\\otimes X\\otimes X\\otimes Y)\\\\\n",
        "&- 0.02869 (I\\otimes I\\otimes I\\otimes I\\otimes Y\\otimes Y\\otimes X\\otimes X) - 0.02869 (I\\otimes I\\otimes I\\otimes I\\otimes X\\otimes X\\otimes Y\\otimes Y)\\\\\n",
        "&+ 0.02869 (I\\otimes I\\otimes I\\otimes I\\otimes X\\otimes Y\\otimes Y\\otimes X) + 0.14139 (I\\otimes I\\otimes I\\otimes I\\otimes Z\\otimes I\\otimes I\\otimes Z)\\\\\n",
        "&+ 0.11270 (I\\otimes I\\otimes I\\otimes I\\otimes I\\otimes Z\\otimes I\\otimes Z) + 0.14139 (I\\otimes I\\otimes I\\otimes I\\otimes I\\otimes Z\\otimes Z\\otimes I)\\\\\n",
        "&+ 0.15436 (I\\otimes I\\otimes I\\otimes I\\otimes I\\otimes I\\otimes Z\\otimes Z)\n",
        "\\end{align*}"
      ],
      "metadata": {
        "id": "sgBR_fcRKAqq"
      },
      "id": "sgBR_fcRKAqq"
    },
    {
      "cell_type": "code",
      "source": [
        "# !pip install -U classiq"
      ],
      "metadata": {
        "id": "bkgCA-XNLBaq"
      },
      "id": "bkgCA-XNLBaq",
      "execution_count": 1,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# import classiq\n",
        "# classiq.authenticate()"
      ],
      "metadata": {
        "id": "jfBwcuM2LdS0"
      },
      "id": "jfBwcuM2LdS0",
      "execution_count": 2,
      "outputs": []
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "id": "e4b9b85d-f164-4c1b-aca3-e24e9a0427f8",
      "metadata": {
        "id": "e4b9b85d-f164-4c1b-aca3-e24e9a0427f8"
      },
      "outputs": [],
      "source": [
        "from typing import List\n",
        "from classiq import *\n",
        "\n",
        "HAMILTONIAN = QConstant(\"HAMILTONIAN\", List[PauliTerm],\n",
        "    [PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.I], coefficient=-73.13873),\n",
        "     PauliTerm(pauli=[Pauli.Z, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.I], coefficient=0.22757),\n",
        "     PauliTerm(pauli=[Pauli.Y, Pauli.Z, Pauli.Z, Pauli.Z, Pauli.Y, Pauli.I, Pauli.I, Pauli.I], coefficient=-0.04375),\n",
        "     PauliTerm(pauli=[Pauli.X, Pauli.Z, Pauli.Z, Pauli.Z, Pauli.X, Pauli.I, Pauli.I, Pauli.I], coefficient=-0.04375),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.Z, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.I], coefficient=0.17420),\n",
        "     PauliTerm(pauli=[Pauli.Z, Pauli.I, Pauli.Z, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.I], coefficient=0.16757),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.Z, Pauli.I, Pauli.I, Pauli.I], coefficient=-0.15961),\n",
        "     PauliTerm(pauli=[Pauli.Z, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.I], coefficient=0.11952),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.Z, Pauli.I], coefficient=-0.18067),\n",
        "     PauliTerm(pauli=[Pauli.Z, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.Z, Pauli.I], coefficient=0.13402),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.Z, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.I], coefficient=0.22757),\n",
        "     PauliTerm(pauli=[Pauli.Z, Pauli.Z, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.I], coefficient=0.19393),\n",
        "     PauliTerm(pauli=[Pauli.Y, Pauli.I, Pauli.Z, Pauli.Z, Pauli.Y, Pauli.I, Pauli.I, Pauli.I], coefficient=-0.03079),\n",
        "     PauliTerm(pauli=[Pauli.X, Pauli.I, Pauli.Z, Pauli.Z, Pauli.X, Pauli.I, Pauli.I, Pauli.I], coefficient=-0.03079),\n",
        "     PauliTerm(pauli=[Pauli.Y, Pauli.X, Pauli.X, Pauli.Y, Pauli.I, Pauli.I, Pauli.I, Pauli.I], coefficient=0.01387),\n",
        "     PauliTerm(pauli=[Pauli.Y, Pauli.Y, Pauli.X, Pauli.X, Pauli.I, Pauli.I, Pauli.I, Pauli.I], coefficient=-0.01387),\n",
        "     PauliTerm(pauli=[Pauli.X, Pauli.X, Pauli.Y, Pauli.Y, Pauli.I, Pauli.I, Pauli.I, Pauli.I], coefficient=-0.01387),\n",
        "     PauliTerm(pauli=[Pauli.X, Pauli.Y, Pauli.Y, Pauli.X, Pauli.I, Pauli.I, Pauli.I, Pauli.I], coefficient=0.01387),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.Y, Pauli.Z, Pauli.Z, Pauli.Z, Pauli.Y, Pauli.I, Pauli.I], coefficient=-0.04375),\n",
        "     PauliTerm(pauli=[Pauli.Z, Pauli.Y, Pauli.Z, Pauli.Z, Pauli.Z, Pauli.Y, Pauli.I, Pauli.I], coefficient=-0.03079),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.X, Pauli.Z, Pauli.Z, Pauli.Z, Pauli.X, Pauli.I, Pauli.I], coefficient=-0.04375),\n",
        "     PauliTerm(pauli=[Pauli.Z, Pauli.X, Pauli.Z, Pauli.Z, Pauli.Z, Pauli.X, Pauli.I, Pauli.I], coefficient=-0.03079),\n",
        "     PauliTerm(pauli=[Pauli.Y, Pauli.X, Pauli.I, Pauli.I, Pauli.X, Pauli.Y, Pauli.I, Pauli.I], coefficient=0.01783),\n",
        "     PauliTerm(pauli=[Pauli.Y, Pauli.Y, Pauli.I, Pauli.I, Pauli.X, Pauli.X, Pauli.I, Pauli.I], coefficient=-0.01783),\n",
        "     PauliTerm(pauli=[Pauli.X, Pauli.X, Pauli.I, Pauli.I, Pauli.Y, Pauli.Y, Pauli.I, Pauli.I], coefficient=-0.01783),\n",
        "     PauliTerm(pauli=[Pauli.X, Pauli.Y, Pauli.I, Pauli.I, Pauli.Y, Pauli.X, Pauli.I, Pauli.I], coefficient=0.01783),\n",
        "     PauliTerm(pauli=[Pauli.Y, Pauli.X, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.X, Pauli.Y], coefficient=0.01737),\n",
        "     PauliTerm(pauli=[Pauli.Y, Pauli.Y, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.X, Pauli.X], coefficient=-0.01737),\n",
        "     PauliTerm(pauli=[Pauli.X, Pauli.X, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.Y, Pauli.Y], coefficient=-0.01737),\n",
        "     PauliTerm(pauli=[Pauli.X, Pauli.Y, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.Y, Pauli.X], coefficient=0.01737),\n",
        "     PauliTerm(pauli=[Pauli.Y, Pauli.Z, Pauli.I, Pauli.Z, Pauli.Y, Pauli.I, Pauli.I, Pauli.I], coefficient=-0.03010),\n",
        "     PauliTerm(pauli=[Pauli.X, Pauli.Z, Pauli.I, Pauli.Z, Pauli.X, Pauli.I, Pauli.I, Pauli.I], coefficient=-0.03010),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.I, Pauli.Z, Pauli.I, Pauli.I, Pauli.I, Pauli.I], coefficient=0.17420),\n",
        "     PauliTerm(pauli=[Pauli.Z, Pauli.I, Pauli.I, Pauli.Z, Pauli.I, Pauli.I, Pauli.I, Pauli.I], coefficient=0.18144),\n",
        "     PauliTerm(pauli=[Pauli.Y, Pauli.Z, Pauli.Z, Pauli.I, Pauli.Y, Pauli.I, Pauli.I, Pauli.I], coefficient=-0.02981),\n",
        "     PauliTerm(pauli=[Pauli.X, Pauli.Z, Pauli.Z, Pauli.I, Pauli.X, Pauli.I, Pauli.I, Pauli.I], coefficient=-0.02981),\n",
        "     PauliTerm(pauli=[Pauli.Y, Pauli.Z, Pauli.Y, Pauli.Y, Pauli.Z, Pauli.Y, Pauli.I, Pauli.I], coefficient=-0.00029),\n",
        "     PauliTerm(pauli=[Pauli.Y, Pauli.Z, Pauli.Y, Pauli.X, Pauli.Z, Pauli.X, Pauli.I, Pauli.I], coefficient=-0.00029),\n",
        "     PauliTerm(pauli=[Pauli.X, Pauli.Z, Pauli.X, Pauli.Y, Pauli.Z, Pauli.Y, Pauli.I, Pauli.I], coefficient=-0.00029),\n",
        "     PauliTerm(pauli=[Pauli.X, Pauli.Z, Pauli.X, Pauli.X, Pauli.Z, Pauli.X, Pauli.I, Pauli.I], coefficient=-0.00029),\n",
        "     PauliTerm(pauli=[Pauli.Y, Pauli.Z, Pauli.Z, Pauli.Z, Pauli.Y, Pauli.I, Pauli.Z, Pauli.I], coefficient=-0.02510),\n",
        "     PauliTerm(pauli=[Pauli.X, Pauli.Z, Pauli.Z, Pauli.Z, Pauli.X, Pauli.I, Pauli.Z, Pauli.I], coefficient=-0.02510),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.Z, Pauli.I, Pauli.I], coefficient=-0.15961),\n",
        "     PauliTerm(pauli=[Pauli.Z, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.Z, Pauli.I, Pauli.I], coefficient=0.13735),\n",
        "     PauliTerm(pauli=[Pauli.Y, Pauli.Z, Pauli.Z, Pauli.Z, Pauli.Y, Pauli.Z, Pauli.I, Pauli.I], coefficient=-0.01131),\n",
        "     PauliTerm(pauli=[Pauli.X, Pauli.Z, Pauli.Z, Pauli.Z, Pauli.X, Pauli.Z, Pauli.I, Pauli.I], coefficient=-0.01131),\n",
        "     PauliTerm(pauli=[Pauli.Y, Pauli.Z, Pauli.Z, Pauli.Z, Pauli.Z, Pauli.X, Pauli.X, Pauli.Y], coefficient=-0.01456),\n",
        "     PauliTerm(pauli=[Pauli.Y, Pauli.Z, Pauli.Z, Pauli.Z, Pauli.Z, Pauli.Y, Pauli.X, Pauli.X], coefficient=0.01456),\n",
        "     PauliTerm(pauli=[Pauli.X, Pauli.Z, Pauli.Z, Pauli.Z, Pauli.Z, Pauli.X, Pauli.Y, Pauli.Y], coefficient=0.01456),\n",
        "     PauliTerm(pauli=[Pauli.X, Pauli.Z, Pauli.Z, Pauli.Z, Pauli.Z, Pauli.Y, Pauli.Y, Pauli.X], coefficient=-0.01456),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.Z], coefficient=-0.18067),\n",
        "     PauliTerm(pauli=[Pauli.Z, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.Z], coefficient=0.15138),\n",
        "     PauliTerm(pauli=[Pauli.Y, Pauli.Z, Pauli.Z, Pauli.Z, Pauli.Y, Pauli.I, Pauli.I, Pauli.Z], coefficient=-0.01054),\n",
        "     PauliTerm(pauli=[Pauli.X, Pauli.Z, Pauli.Z, Pauli.Z, Pauli.X, Pauli.I, Pauli.I, Pauli.Z], coefficient=-0.01054),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.Z, Pauli.I, Pauli.Z, Pauli.I, Pauli.I, Pauli.I, Pauli.I], coefficient=0.16757),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.Z, Pauli.I, Pauli.I, Pauli.I, Pauli.Z, Pauli.I, Pauli.I], coefficient=0.11952),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.Z, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.Z], coefficient=0.13402),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.Z, Pauli.Z, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.I], coefficient=0.18144),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.Y, Pauli.I, Pauli.Z, Pauli.Z, Pauli.Y, Pauli.I, Pauli.I], coefficient=-0.02981),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.X, Pauli.I, Pauli.Z, Pauli.Z, Pauli.X, Pauli.I, Pauli.I], coefficient=-0.02981),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.Y, Pauli.X, Pauli.X, Pauli.Y, Pauli.I, Pauli.I, Pauli.I], coefficient=-0.00029),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.Y, Pauli.Y, Pauli.X, Pauli.X, Pauli.I, Pauli.I, Pauli.I], coefficient=0.00029),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.X, Pauli.X, Pauli.Y, Pauli.Y, Pauli.I, Pauli.I, Pauli.I], coefficient=0.00029),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.X, Pauli.Y, Pauli.Y, Pauli.X, Pauli.I, Pauli.I, Pauli.I], coefficient=-0.00029),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.Y, Pauli.Z, Pauli.I, Pauli.Z, Pauli.Y, Pauli.I, Pauli.I], coefficient=-0.03010),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.X, Pauli.Z, Pauli.I, Pauli.Z, Pauli.X, Pauli.I, Pauli.I], coefficient=-0.03010),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.Z, Pauli.I, Pauli.I, Pauli.Z, Pauli.I, Pauli.I, Pauli.I], coefficient=0.13735),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.Y, Pauli.Z, Pauli.Z, Pauli.I, Pauli.Y, Pauli.I, Pauli.I], coefficient=-0.01131),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.X, Pauli.Z, Pauli.Z, Pauli.I, Pauli.X, Pauli.I, Pauli.I], coefficient=-0.01131),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.Y, Pauli.Z, Pauli.Z, Pauli.X, Pauli.I, Pauli.X, Pauli.Y], coefficient=0.01456),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.Y, Pauli.Z, Pauli.Z, Pauli.Y, Pauli.I, Pauli.Y, Pauli.Y], coefficient=0.01456),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.X, Pauli.Z, Pauli.Z, Pauli.X, Pauli.I, Pauli.X, Pauli.X], coefficient=0.01456),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.X, Pauli.Z, Pauli.Z, Pauli.Y, Pauli.I, Pauli.Y, Pauli.X], coefficient=0.01456),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.Y, Pauli.Z, Pauli.Z, Pauli.Z, Pauli.Y, Pauli.I, Pauli.Z], coefficient=-0.02510),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.X, Pauli.Z, Pauli.Z, Pauli.Z, Pauli.X, Pauli.I, Pauli.Z], coefficient=-0.02510),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.Z, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.Z, Pauli.I], coefficient=0.15138),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.Y, Pauli.Z, Pauli.Z, Pauli.Z, Pauli.Y, Pauli.Z, Pauli.I], coefficient=-0.01054),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.X, Pauli.Z, Pauli.Z, Pauli.Z, Pauli.X, Pauli.Z, Pauli.I], coefficient=-0.01054),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.Z, Pauli.I, Pauli.Z, Pauli.I, Pauli.I, Pauli.I], coefficient=0.13767),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.Z, Pauli.I, Pauli.I, Pauli.I, Pauli.Z, Pauli.I], coefficient=0.14973),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.Z, Pauli.Z, Pauli.I, Pauli.I, Pauli.I, Pauli.I], coefficient=0.22004),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.Y, Pauli.X, Pauli.X, Pauli.Y, Pauli.I, Pauli.I], coefficient=0.00956),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.Y, Pauli.Y, Pauli.X, Pauli.X, Pauli.I, Pauli.I], coefficient=-0.00956),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.X, Pauli.X, Pauli.Y, Pauli.Y, Pauli.I, Pauli.I], coefficient=-0.00956),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.X, Pauli.Y, Pauli.Y, Pauli.X, Pauli.I, Pauli.I], coefficient=0.00956),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.Y, Pauli.X, Pauli.I, Pauli.I, Pauli.X, Pauli.Y], coefficient=0.00609),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.Y, Pauli.Y, Pauli.I, Pauli.I, Pauli.X, Pauli.X], coefficient=-0.00609),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.X, Pauli.X, Pauli.I, Pauli.I, Pauli.Y, Pauli.Y], coefficient=-0.00609),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.X, Pauli.Y, Pauli.I, Pauli.I, Pauli.Y, Pauli.X], coefficient=0.00609),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.Z, Pauli.I, Pauli.I, Pauli.Z, Pauli.I, Pauli.I], coefficient=0.14723),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.Z, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.Z], coefficient=0.15582),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.I, Pauli.Z, Pauli.I, Pauli.Z, Pauli.I, Pauli.I], coefficient=0.13767),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.I, Pauli.Z, Pauli.I, Pauli.I, Pauli.I, Pauli.Z], coefficient=0.14973),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.I, Pauli.Z, Pauli.Z, Pauli.I, Pauli.I, Pauli.I], coefficient=0.14723),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.I, Pauli.Z, Pauli.I, Pauli.I, Pauli.Z, Pauli.I], coefficient=0.15582),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.Z, Pauli.I, Pauli.Z, Pauli.I], coefficient=0.11270),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.Z, Pauli.Z, Pauli.I, Pauli.I], coefficient=0.14926),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.Y, Pauli.X, Pauli.X, Pauli.Y], coefficient=0.02869),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.Y, Pauli.Y, Pauli.X, Pauli.X], coefficient=-0.02869),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.X, Pauli.X, Pauli.Y, Pauli.Y], coefficient=-0.02869),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.X, Pauli.Y, Pauli.Y, Pauli.X], coefficient=0.02869),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.Z, Pauli.I, Pauli.I, Pauli.Z], coefficient=0.14139),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.Z, Pauli.I, Pauli.Z], coefficient=0.11270),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.Z, Pauli.Z, Pauli.I], coefficient=0.14139),\n",
        "     PauliTerm(pauli=[Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.I, Pauli.Z, Pauli.Z], coefficient=0.15436)])"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "@qfunc\n",
        "def main(q: Output[QArray[QBit]], angles: CArray[CReal, 6]) -> None:\n",
        "    allocate(8, q)\n",
        "    # U(angles[0], angles[1], angles[2], 0, q[0])\n",
        "    # U(angles[3], angles[4], angles[5], 0, q[1])\n",
        "    # U(angles[6], angles[7], angles[8], 0, q[2])\n",
        "    # U(angles[9], angles[10], angles[11], 0, q[3])\n",
        "    # U(angles[12], angles[13], angles[14], 0, q[4])\n",
        "    # U(angles[15], angles[16], angles[17], 0, q[5])\n",
        "    # U(angles[18], angles[19], angles[20], 0, q[6])\n",
        "    # U(angles[21], angles[22], angles[23], 0, q[7])\n",
        "    repeat(q.len, lambda i: U(angles[0], angles[1], angles[2], 0, q[i]))\n",
        "    repeat(q.len-1, lambda i: repeat(q.len-i-1, lambda j: CX(q[i], q[i+j+1])))\n",
        "    repeat(q.len, lambda i: U(angles[3], angles[4], angles[5], 0, q[i]))\n",
        "    # repeat(q.len-1, lambda i: repeat(q.len-i-1, lambda j: CX(q[i], q[i+j+1])))\n",
        "    # repeat(q.len, lambda i: U(angles[6], angles[7], angles[8], 0, q[i]))\n",
        "\n",
        "\n",
        "@cfunc\n",
        "def cmain() -> None:\n",
        "    res = vqe(\n",
        "        HAMILTONIAN,\n",
        "        False,\n",
        "        [],\n",
        "        optimizer=Optimizer.COBYLA,\n",
        "        max_iteration=5000,\n",
        "        tolerance=0.001,\n",
        "        step_size=0,\n",
        "        skip_compute_variance=False,\n",
        "        alpha_cvar=1.0,\n",
        "    )\n",
        "    save({\"result\": res})\n",
        "\n",
        "qmod = create_model(main, classical_execution_function=cmain)\n",
        "qprog = synthesize(qmod)\n",
        "# show(qprog)"
      ],
      "metadata": {
        "id": "pm3NUPq7K7Xd"
      },
      "id": "pm3NUPq7K7Xd",
      "execution_count": 4,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "execution = execute(qprog)\n",
        "res = execution.result()\n",
        "# execution.open_in_ide()\n",
        "vqe_result = res[0].value"
      ],
      "metadata": {
        "id": "sTTUaPmKLW5O"
      },
      "id": "sTTUaPmKLW5O",
      "execution_count": 5,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "print(f\"Optimal energy: {vqe_result.energy}\")\n",
        "print(f\"Optimal parameters: {vqe_result.optimal_parameters}\")\n",
        "print(f\"Eigenstate: {vqe_result.eigenstate}\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "y2LsEoJQL6KS",
        "outputId": "f97511b4-26f0-4032-f737-0338a207238e"
      },
      "id": "y2LsEoJQL6KS",
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Optimal energy: -74.28639479492185\n",
            "Optimal parameters: {'angles_0': -1.622441216459358, 'angles_1': 6.273309048672006, 'angles_2': 2.393676433518781, 'angles_3': -1.5671779543739635, 'angles_4': 1.8858586567955338, 'angles_5': -0.026747140832290826}\n",
            "Eigenstate: {'10101001': (0.02209708691207961+0j), '10001010': (0.02209708691207961+0j), '10100010': (0.02209708691207961+0j), '10101000': (0.02209708691207961+0j), '10101101': (0.02209708691207961+0j), '10110101': (0.02209708691207961+0j), '10100101': (0.03125+0j), '10101011': (0.038273277230987154+0j), '10101010': (0.9973108373270593+0j)}\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "Wc7QKyY6MDvu"
      },
      "id": "Wc7QKyY6MDvu",
      "execution_count": 6,
      "outputs": []
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3 (ipykernel)",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.11.8"
    },
    "colab": {
      "provenance": []
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}